package com.liam.chatroom;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.*;
import java.net.Socket;
import java.util.Properties;

public class ClientChatMain extends JFrame implements ActionListener , KeyListener {

    // 文本
    private JTextArea textArea;

    // 滚动条
    private JScrollPane scrollPane;

    // 面板
    private JPanel panel;

    // 文本框
    private JTextField textField;

    // 按钮
    private JButton button;

    private BufferedWriter bw = null;

    private static  int serverPort;
    private static String serverIp ;

    static {
        Properties properties =  new Properties();
        try {
            properties.load(new FileReader(ClientChatMain.class.getClassLoader().getResource("chat.properties").getPath()));

            serverPort = Integer.valueOf(properties.getProperty("server.port"));
            serverIp = properties.getProperty("server.ip");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public ClientChatMain(){
        textArea = new JTextArea();
        textArea.setEditable(false);

        // 将文本添加到滚动条
        scrollPane = new JScrollPane(textArea);

        panel = new JPanel();
        textField = new JTextField(10);
        textField.addKeyListener(this);

        button = new JButton("send");

        // 添加到面板
        panel.add(textField);
        panel.add(button);

        this.add(scrollPane, BorderLayout.CENTER);
        this.add(panel, BorderLayout.SOUTH);

        this.setTitle("聊天室-客户端");
        this.setSize(300,300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);

        /**  tcp 客户端 begin   **/

        button.addActionListener(this);


        try {
            // 创建服务端socket
            Socket socket = new Socket(serverIp,serverPort);

            // 获取输入流
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));

            // 获取输出流
            bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));


            // 获取内容
            String line = null;
            while ((line = br.readLine()) != null){
                textArea.append(line + System.lineSeparator());
            }

            // 关闭通道
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        /**  tcp 客户端 end   **/
    }

    public static void main(String[] args) {
        new ClientChatMain();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String content = textField.getText();
        content = "client:"+content ;

        textArea.append(content + System.lineSeparator());

        try {
            bw.write(content);
            bw.newLine();
            bw.flush();
            textField.setText("");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if(e.getKeyCode() == KeyEvent.VK_ENTER){
            actionPerformed(null);
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
